#include "lcd_init.h"
#include "stdio.h"

typedef void (*FunType)(void);

void wr_cmd_4spi_8bcs(uint8_t par)
{
    LCD_DCXLow();
    LCD_CSLow();

    LCD_SCLLow();
    if (par & 0x80)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x40)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x20)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x10)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x08)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x04)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x02)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x01)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_CSHigh();
    LCD_DCXHigh();
    HAL_Delay(5);
}
void wr_dat_4spi_8bcs(uint8_t par)
{
    LCD_DCXHigh();
    LCD_CSLow();

    LCD_SCLLow();
    if (par & 0x80)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x40)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x20)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x10)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x08)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x04)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x02)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x01)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_CSHigh();
    LCD_DCXHigh();
    HAL_Delay(1);
}
void wr_cmd_parall1_8(uint16_t par)
{
    LCD_DCXLow();
    LCD_CSLow();
    LCD_WRLow();
    GPIOB->ODR = par;

    LCD_WRHigh();
}
void wr_dat_parall1_8(uint16_t par)
{
    LCD_DCXHigh();
    LCD_WRLow();
    GPIOB->ODR = par;

    LCD_WRHigh();
}
void wr_cmd_parall1_16(uint16_t par)
{
    LCD_DCXLow();
    LCD_CSLow();
    LCD_WRLow();
    GPIOB->ODR = par;

    LCD_WRHigh();
}
void wr_dat_parall1_16(uint16_t par)
{
    LCD_DCXHigh();
    LCD_WRLow();
    GPIOB->ODR = par;

    LCD_WRHigh();
}
void wr_displ_parall1_18(uint16_t color1, uint16_t color2)
{
    LCD_DCXHigh();
    LCD_WRLow();
    if (color1 & 0x02)
        HAL_GPIO_WritePin(DB17_GPIO_Port, DB17_Pin, GPIO_PIN_SET);
    else
        HAL_GPIO_WritePin(DB17_GPIO_Port, DB17_Pin, GPIO_PIN_RESET);
    if (color1 & 0x01)
        HAL_GPIO_WritePin(DB16_GPIO_Port, DB16_Pin, GPIO_PIN_SET);
    else
        HAL_GPIO_WritePin(DB16_GPIO_Port, DB16_Pin, GPIO_PIN_RESET);
    GPIOB->ODR = color2;
    LCD_WRHigh();
}

void HW_Reset(void)
{
    LCD_RSTHigh();
    HAL_Delay(120);
    LCD_RSTLow();
    HAL_Delay(120);
    LCD_RSTHigh();
    HAL_Delay(200);
}

void Delay(uint32_t ms)
{
    HAL_Delay(ms);
}

void Write(uint8_t type, uint16_t dat)
{
    if (type == 0)
    {
        wr_cmd_parall1_8(dat);
    }
    else
    {
        wr_dat_parall1_8(dat);
    }
}

void YTC272210LAA_246_100N(void)
{
    uint16_t h_offset = 0x18;
    /****************并口0-7bit*****************/
    HW_Reset();

    Delay(120); // ms

    Write(Command, 0x36);
    Write(Parameter, 0x48);

    Write(Command, 0x3A);
    Write(Parameter, 0x77);

    Write(Command, 0xF0);
    Write(Parameter, 0xC3);

    Write(Command, 0xF0);
    Write(Parameter, 0x96);

    Write(Command, 0xB4);
    Write(Parameter, 0x01);

    Write(Command, 0xB7);
    Write(Parameter, 0xC6);

    Write(Command, 0xB9);
    Write(Parameter, 0x02);
    Write(Parameter, 0xE0);

    Write(Command, 0xC0);
    Write(Parameter, 0x80);
    Write(Parameter, 0x36);

    Write(Command, 0xC1);
    Write(Parameter, 0x19);

    Write(Command, 0xC2);
    Write(Parameter, 0xAF);

    Write(Command, 0xC5);
    Write(Parameter, 0x0E);

    Write(Command, 0xE8);
    Write(Parameter, 0x40);
    Write(Parameter, 0x8A);
    Write(Parameter, 0x00);
    Write(Parameter, 0x00);
    Write(Parameter, 0x29);
    Write(Parameter, 0x19);
    Write(Parameter, 0xA5);
    Write(Parameter, 0x33);

    Write(Command, 0xE0);
    Write(Parameter, 0xF0);
    Write(Parameter, 0x0A);
    Write(Parameter, 0x12);
    Write(Parameter, 0x08);
    Write(Parameter, 0x0B);
    Write(Parameter, 0x16);
    Write(Parameter, 0x3B);
    Write(Parameter, 0x44);
    Write(Parameter, 0x50);
    Write(Parameter, 0x39);
    Write(Parameter, 0x14);
    Write(Parameter, 0x14);
    Write(Parameter, 0x2E);
    Write(Parameter, 0x35);

    Write(Command, 0xE1);
    Write(Parameter, 0xF0);
    Write(Parameter, 0x15);
    Write(Parameter, 0x19);
    Write(Parameter, 0x0E);
    Write(Parameter, 0x09);
    Write(Parameter, 0x07);
    Write(Parameter, 0x3B);
    Write(Parameter, 0x43);
    Write(Parameter, 0x4F);
    Write(Parameter, 0x36);
    Write(Parameter, 0x12);
    Write(Parameter, 0x12);
    Write(Parameter, 0x2F);
    Write(Parameter, 0x33);

    Write(Command, 0x21);

    Write(Command, 0x2A); // 272
    Write(Parameter, 0x00);
    Write(Parameter, 0x00+h_offset);
    Write(Parameter, 0x01);
    Write(Parameter, 0x0F+h_offset);

    Write(Command, 0x2B); // 480
    Write(Parameter, 0x00);
    Write(Parameter, 0x00);
    Write(Parameter, 0x01);
    Write(Parameter, 0xDF);

    Write(Command, 0xF0);
    Write(Parameter, 0xC3);

    Write(Command, 0xF0);
    Write(Parameter, 0x69);

    Write(Command, 0x11);

    Delay(120); // ms

    Write(Command, 0x29);
}
// void Filllcd_Area(uint16_t _usX, uint16_t _usY, uint16_t _usWidth , uint16_t _usHeight, uint16_t color)
void Filllcd_Area(uint16_t color)
{
    uint32_t x = 0;
    uint32_t y = 0;
    /*//显示区域限制
    wr_cmd_4spi_8bcs(GC9A01_CMD_CASETF);
    wr_dat_4spi_8bcs(_usX >> 8);
    wr_dat_4spi_8bcs(_usX );
    wr_dat_4spi_8bcs( (_usX + _usWidth - 1) >> 8);
    wr_dat_4spi_8bcs( (_usX + _usWidth - 1) );

    wr_cmd_4spi_8bcs(GC9A01_CMD_RASETF );
    wr_dat_4spi_8bcs(_usY >> 8);
    wr_dat_4spi_8bcs(_usY );
    wr_dat_4spi_8bcs( (_usY + _usHeight - 1) >> 8 );
    wr_dat_4spi_8bcs( (_usY + _usHeight - 1) );
    //准备接收数据*/
    /*wr_cmd_4spi_8bcs(0x2c);

    //uint8_t colorred = color >> 16;
    uint8_t color1 = (color & 0xff00) >> 8;
    uint8_t color2 = (color & 0xff);
    //uint8_t color1,color2;
    for( y=0; y < LCD_Height; y++ )
        {for( x=0; x < LCD_Width; x++ )
        {
            wr_dat_4spi_8bcs( 0xf8);
            wr_dat_4spi_8bcs( 0x00 );
        }
    }*/
    /*wr_c2c();

    for( y=0; y < LCD_Height ; y++ )
        {for( x=0; x < LCD_Width; x++ )
        {
            wr_num(0xf8);
            wr_num(0x00);
        }
    }*/
    wr_cmd_parall1_8(0X2C);

    for (y = 0; y < LCD_Height; y++)
    {
        for (x = 0; x < LCD_Width; x++)
        {
            wr_displ_parall1_18(0x0003, 0xF000);
            // wr_displ_parall2_18_H(0x0003);
            // wr_displ_parall2_18_L(0xF000);
        }
    }
}